﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;

using CE.User;
using CE.Global;
using CE.Database;
using CE.CarInsurance;

public partial class User_InsuranceDetails : MyLanguageBasePage
{
    string _viewciresultid = "";
    Int64 _tempResultId = 0;
    Int64 _carId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("login.aspx");
        }

        if (Request.QueryString["vcid"] != null)
        {
            _viewciresultid = Request.QueryString["vcid"].ToString().Trim();
        }
        else
        {
            if (Request.QueryString["cid"] != null)
            {
                if (Request.QueryString["cid"].ToString() != "")
                _carId = int.Parse(Request.QueryString["cid"].ToString());
            }
        }
        
        if (_viewciresultid != "")     
        {
            
            if (Int64.TryParse(_viewciresultid, out _tempResultId))   
            {
                _carId = CheckCompleteStatus(_tempResultId);

                if (_carId > 0)
                {
                    ShowDetails(_carId);
                }
                else
                {
                    Response.Redirect("user_details.aspx");
                }
            }
            else
            {
                Response.Redirect("user_details.aspx");
            }
        }
        else if (_carId > 0)
        {
            ShowDetails(_carId);
        }
        else
        {
            Response.Redirect("user_details.aspx");
        }

    }

    private void ShowNonGGDetails(DataSet DsNonGG)
    {
        if (DsNonGG != null && DsNonGG.Tables.Count > 2 && DsNonGG.Tables[2].Rows.Count > 0)
        {
            Int64 _refCEUserId = Convert.ToInt64(Convert.ToInt64(Session[General.UserId].ToString()));
            string _refNo = "";
            Int64 _companyId = Convert.ToInt64(DsNonGG.Tables[2].Rows[0]["ProviderCompanyId"].ToString());
            lblAnnualPremium.Text = "$ " + Convert.ToSingle(DsNonGG.Tables[2].Rows[0]["TotalCollectedPremium"].ToString()).ToString("#.00");

            lblGender.Text = DsNonGG.Tables[2].Rows[0]["Gender"].ToString() == "M" ? Resources.CarInsurance.Male : Resources.CarInsurance.Female;
            lblDOB.Text = Convert.ToDateTime(DsNonGG.Tables[0].Rows[0]["DOB"]).ToString("dd-MMM-yyyy");
            if (CE.Global.General.SetCulture() == "th-TH")
            {
                lblOccupation.Text = DsNonGG.Tables[2].Rows[0]["OccupationTH"].ToString();
            }
            else
            {
                lblOccupation.Text = DsNonGG.Tables[2].Rows[0]["OccupationEN"].ToString();
            }
            //switch (DsNonGG.Tables[2].Rows[0]["InsuranceType"].ToString())
            //{
            //    case "10":
            //        lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassOne;
            //        break;
            //    case "11":
            //        lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassOneLowCost;
            //        break;
            //    case "21":
            //        lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassTwoPlus;
            //        break;
            //    case "31":
            //        lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassThreePlus;
            //        break;
            //    case "30":
            //        lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassThree;
            //        break;
            //}

            //lblGarageType.Text = DsNonGG.Tables[2].Rows[0]["GarageType"].ToString() == "1" ? Resources.CarInsurance.Garage_Authorized : Resources.CarInsurance.Garage_Dealer;
            //lblCarModelYear.Text = DsNonGG.Tables[2].Rows[0]["CarYear"].ToString();
            lblCarMakeModel.Text = DsNonGG.Tables[2].Rows[0]["CarModel"].ToString();
            //lblVehicleType.Text = DsNonGG.Tables[2].Rows[0]["VehicleType"].ToString() == "110" ? Resources.CarInsurance.VehicleType_Private : Resources.CarInsurance.VehicleType_Commercial;
            //lblRedPlate.Text = DsNonGG.Tables[2].Rows[0]["CarYear"].ToString();
            //lblSumInsured.Text = Convert.ToSingle(DsNonGG.Tables[0].Rows[0]["SumInsured"]).ToString("#,###") + " " + Resources.CarInsurance.Baht;
            //lblProvinceofRegistration.Text = DsNonGG.Tables[2].Rows[0]["CarPlateProvince"].ToString();
            //lblAnyClaim.Text = Resources.CarInsurance.Option_NoHave;
            //if (DsNonGG.Tables[2].Rows[0]["IsClaim"] != DBNull.Value && DsNonGG.Tables[2].Rows[0]["IsClaim"].ToString() != string.Empty)
            //{
            //    lblAnyClaim.Text = DsNonGG.Tables[2].Rows[0]["IsClaim"].ToString() == "1" ? Resources.CarInsurance.Option_YesHave : Resources.CarInsurance.Option_NoHave;
            //}
            //lblNCB.Text = DsNonGG.Tables[2].Rows[0]["NCB"].ToString() + "%";
            //lblAddDriver.Text = DsNonGG.Tables[2].Rows[0]["NoOfDriver"].ToString() != "0" ? Resources.CarInsurance.Option_YesSpecify : Resources.CarInsurance.Option_NoSpecify;
            //lblCoverCommenceDate.Text = Convert.ToDateTime(DsNonGG.Tables[2].Rows[0]["CoverageStart"]).ToString("dd-MMM-yyyy");

            lblInsurer.Text = DsNonGG.Tables[2].Rows[0]["CurrentInsurer"].ToString();
            lblAnnualPremium.Text = Convert.ToSingle(DsNonGG.Tables[2].Rows[0]["TotalCollectedPremium"]).ToString("#,###.00") + " " + Resources.CarInsurance.Baht;
            //lblCTPL.Text = DsNonGG.Tables[2].Rows[0]["IsCTPL"].ToString() == "1" ? Resources.CarInsurance.Option_Yes : Resources.CarInsurance.Option_No;
            lblCTPLAmount.Text = Convert.ToSingle(DsNonGG.Tables[2].Rows[0]["CTPL"]).ToString("#.00") + " " + Resources.CarInsurance.Baht;
            lblPackage.Text = DsNonGG.Tables[2].Rows[0]["Package"].ToString();

            //lblName.Text = DsNonGG.Tables[2].Rows[0]["Name"].ToString();
            //lblIdentificationNo.Text = "N/A";

            //lblContactNo.Text = String.Format("{0:0##-###-####}", Convert.ToInt32(DsNonGG.Tables[2].Rows[0]["ContactNo"]));
            //lblVehicleNo.Text = DsNonGG.Tables[2].Rows[0]["CarPlate"].ToString();
            //lblEngineNo.Text = DsNonGG.Tables[2].Rows[0]["EngineNumber"].ToString();
            //lblChassisNo.Text = DsNonGG.Tables[2].Rows[0]["ChassisNumber"].ToString();
            //lblVehicleProvince.Text = DsNonGG.Tables[2].Rows[0]["CarPlateProvince"].ToString();
            //lblPolicyStartDate.Text = Convert.ToDateTime(DsNonGG.Tables[2].Rows[0]["CTPLStart"]).ToString("dd-MMM-yyyy");
            //_refNo = DsNonGG.Tables[2].Rows[0]["QuoteRefNo"].ToString();

            lblPaymentType.Text = "N/A";
            switch (DsNonGG.Tables[2].Rows[0]["PaymentStatus"].ToString())
            {
                case "1":
                    lblPaymentStatus.Text = Resources.ProposalForm.Yes;
                    break;
                case "0":
                    lblPaymentStatus.Text = Resources.ProposalForm.No;
                    break;
            }
            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : " + _refNo + " - " + Resources.CarInsurance.CarInsuranceList + " " + lblCarMakeModel.Text;

        }
    }

    private void ShowDetails(Int64 _carId)
    {
        Int64 CustomerId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());

        DataSet _dsOffline = new DataSet();
        _dsOffline = GetCarDetails(_carId, CustomerId);
        if (_dsOffline != null && _dsOffline.Tables.Count > 2 && _dsOffline.Tables[2].Rows.Count > 0)
        {
            ShowNonGGDetails(_dsOffline);
            return;
        }
        Int64 _refCEUserId = Convert.ToInt64(Convert.ToInt64(Session[General.UserId].ToString()));
        string _refNo = "";
        CarDetailTH _cardetailth = new CarDetailTH();
        CarDetailTHDB _cardetailthdb = new CarDetailTHDB();
        CE.Database.MyDatabase _db = new CE.Database.MyDatabase();
        DataSet _dsInsurance = new DataSet();

        Int64 _companyId = 0;

        System.Data.DataTable _dtSeletectItem = new System.Data.DataTable("SelectedResultItem");
        _dtSeletectItem = _db.GetDataTable("Select * from tblCarInsuranceSelectItemTH WHERE CarId = " + _carId.ToString());
        if (_dtSeletectItem != null && _dtSeletectItem.Rows.Count > 0)
        {
            _companyId = Convert.ToInt64(_dtSeletectItem.Rows[0]["ProviderCompanyId"].ToString());
            lblAnnualPremium.Text = "$ " + Convert.ToSingle(_dtSeletectItem.Rows[0]["AnnualPremium"].ToString()).ToString("#.00");
        }
        CarInsuranceCompletedItemDB _carinsurancecompleteditemdb = new CarInsuranceCompletedItemDB();
        _dsInsurance = _carinsurancecompleteditemdb.GetCarInsuranceCompletedItem(_carId, _companyId);
        if (_dsInsurance != null && _dsInsurance.Tables[0].Rows.Count > 0)
        {
            lblGender.Text = _dsInsurance.Tables[0].Rows[0]["Gender"].ToString() == "Male" ? Resources.CarInsurance.Male : Resources.CarInsurance.Female;
            lblDOB.Text = Convert.ToDateTime(_dsInsurance.Tables[0].Rows[0]["DOB"]).ToString("dd-MMM-yyyy");
            if (CE.Global.General.SetCulture() == "th-TH")
            {
                lblOccupation.Text = _dsInsurance.Tables[0].Rows[0]["THOccupation"].ToString();
            }
            else
            {
                lblOccupation.Text = _dsInsurance.Tables[0].Rows[0]["ENOccupation"].ToString();
            }
            //lblInsuranceType.Text = _dsInsurance.Tables[0].Rows[0]["InsuranceType"].ToString();
            //switch (_dsInsurance.Tables[0].Rows[0]["InsuranceType"].ToString())
            //{
            //    case "Class 1":
            //        lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassOne;
            //        break;
            //    case "Class 1 (Low Cost)":
            //        lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassOneLowCost;
            //        break;
            //    case "Class 2+":
            //        lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassTwoPlus;
            //        break;
            //    case "Class 3+":
            //        lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassThreePlus;
            //        break;
            //    case "Class 3":
            //        lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassThree;
            //        break;
            //}

            //lblGarageType.Text = _dsInsurance.Tables[0].Rows[0]["GarageType"].ToString() == "Authorized" ? Resources.CarInsurance.Garage_Authorized : Resources.CarInsurance.Garage_Dealer;
            //lblCarModelYear.Text = _dsInsurance.Tables[0].Rows[0]["CarModelYear"].ToString();
            lblCarMakeModel.Text = _dsInsurance.Tables[0].Rows[0]["MakeModelDesc"].ToString();
            //lblVehicleType.Text = _dsInsurance.Tables[0].Rows[0]["VehicleType"].ToString() == "Private Use" ? Resources.CarInsurance.VehicleType_Private : Resources.CarInsurance.VehicleType_Commercial;
            //lblRedPlate.Text = _dsInsurance.Tables[0].Rows[0]["RedPlate"].ToString();
            //lblSumInsured.Text = Convert.ToSingle(_dsInsurance.Tables[0].Rows[0]["SumInsured"]).ToString("#,###") + " " + Resources.CarInsurance.Baht;
            //lblProvinceofRegistration.Text = _dsInsurance.Tables[0].Rows[0]["ProvinceRegistration"].ToString() == "Bangkok Metro Area" ? Resources.CarInsurance.Province_BKK : Resources.CarInsurance.Province_UPC;
            //lblAnyClaim.Text = _dsInsurance.Tables[0].Rows[0]["AnyClaim"].ToString() == "Yes" ? Resources.CarInsurance.Option_YesHave : Resources.CarInsurance.Option_NoHave;
            //lblNCB.Text = _dsInsurance.Tables[0].Rows[0]["NCB"].ToString() + "%";
            //lblAddDriver.Text = _dsInsurance.Tables[0].Rows[0]["Driver"].ToString() == "Yes" ? Resources.CarInsurance.Option_YesSpecify : Resources.CarInsurance.Option_NoSpecify;
            //lblCoverCommenceDate.Text = Convert.ToDateTime(_dsInsurance.Tables[0].Rows[0]["CommenceDate"]).ToString("dd-MMM-yyyy");

            if (_dsInsurance.Tables[1].Rows.Count > 0)
            {
                lblInsurer.Text = CE.Global.General.GetProviderName(_companyId, "2");
                lblAnnualPremium.Text = Convert.ToSingle(_dsInsurance.Tables[1].Rows[0]["AnnualPremium"]).ToString("#,###.00") + " " + Resources.CarInsurance.Baht;
                //lblCTPL.Text = _dsInsurance.Tables[1].Rows[0]["CTPL"].ToString() == "Yes" ? Resources.CarInsurance.Option_Yes : Resources.CarInsurance.Option_No;
                lblCTPLAmount.Text = Convert.ToSingle(_dsInsurance.Tables[1].Rows[0]["CTPLAmount"]).ToString("#.00") + " " + Resources.CarInsurance.Baht;
                lblPackage.Text = _dsInsurance.Tables[1].Rows[0]["CoverTypeDesc"].ToString();

                //lblName.Text = _dsInsurance.Tables[1].Rows[0]["Name"].ToString();
                //lblIdentificationNo.Text = "";
                //if (_dsInsurance.Tables[1].Rows[0]["IdentificationNo"] != null)
                //{
                //    lblIdentificationNo.Text = _dsInsurance.Tables[1].Rows[0]["IdentificationNo"].ToString();
                //}
                //lblContactNo.Text = String.Format("{0:0##-###-####}", Convert.ToInt32(_dsInsurance.Tables[1].Rows[0]["ContactNo"]));
                //lblVehicleNo.Text = _dsInsurance.Tables[1].Rows[0]["VehicleNo"].ToString();
                //lblEngineNo.Text = _dsInsurance.Tables[1].Rows[0]["EngineNo"].ToString();
                //lblChassisNo.Text = _dsInsurance.Tables[1].Rows[0]["ChassisNo"].ToString();
                //lblVehicleColor.Text = _dsInsurance.Tables[1].Rows[0]["VehicleColor"].ToString();
                //lblVehicleProvince.Text = _dsInsurance.Tables[1].Rows[0]["VehicleProvince"].ToString();
                //lblPolicyStartDate.Text = Convert.ToDateTime(_dsInsurance.Tables[1].Rows[0]["PolicyStartDate"]).ToString("dd-MMM-yyyy");
                _refNo = _dsInsurance.Tables[1].Rows[0]["QuoteRefNo"].ToString();

                //Sakthi on 12 Jan 2012  -- Start
                switch (_dsInsurance.Tables[1].Rows[0]["PaymentType"].ToString())
                {
                    case "CreditCard":
                        lblPaymentType.Text = Resources.ProposalForm.CreditCard;
                        break;
                    case "Bank In":
                        lblPaymentType.Text = Resources.ProposalForm.BankIn;
                        break;
                }
                switch (_dsInsurance.Tables[1].Rows[0]["PaymentStatus"].ToString())
                {
                    case "True":
                        lblPaymentStatus.Text = Resources.ProposalForm.Yes;
                        break;
                    case "False":
                        lblPaymentStatus.Text = Resources.ProposalForm.No;
                        break;
                }
                if (_dsInsurance.Tables[1].Rows[0]["PaymentType"].ToString() == "CreditCard")
                {
                    switch (_dsInsurance.Tables[1].Rows[0]["PaymentCode"].ToString())
                    {
                        case "0":
                            lblPaymentStatus.Text += " - " + Resources.ProposalForm.Fail;
                            break;
                        case "1":
                            lblPaymentStatus.Text += " - " + Resources.ProposalForm.Success;
                            break;
                        case "2":
                            lblPaymentStatus.Text += " - " + Resources.ProposalForm.Cancel;
                            break;
                    }
                }
                //Sakthi on 12 Jan 2012  -- End
            }
            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : " + _refNo + " - " + Resources.CarInsurance.CarInsuranceList +" " + lblCarMakeModel.Text;
        }
    }

    public DataSet GetCarDetails(Int64 CarId, Int64 CustomerId)
    {
        MyDatabase _db = new MyDatabase();
        string _spName = "[sprocCXCRMNONGGPackageCarDetailsSelectedItemByCarId]";

        SortedList _spara = new SortedList();
        _spara.Add("@CarId", CarId);
        _spara.Add("@CustomerId", CustomerId);

        DataSet _ds = _db.GetDataSet(_spName, _spara);

        return _ds;
    }

    private Int64 CheckCompleteStatus(Int64 _resultId)
    {
        UserResultHistory _result = new UserResultHistory();
        UserResultHistoryDB _resultdb = new UserResultHistoryDB();
        Int64 _carId = 0;
        Int64 CustomerId = 0;
        if (Session[CE.Global.General.UserId] != null)
        {
            CustomerId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
        }
        _result = _resultdb.GetResultHistory(Convert.ToInt64(Session[General.UserId].ToString()), _resultId);
        if (_result != null && _result.IsComplete)
        {
            _carId = _result.ID1;
        }
        else
        {
            DataSet _ds = GetCarDetails(_result.ID1, CustomerId);
            if (_ds != null && _ds.Tables.Count > 2 && _ds.Tables[2].Rows.Count > 0)
            {
                _carId = _result.ID1;
            }
        }

        return _carId;
    }

}
